
import React, { useState, useEffect, useRef,useLayoutEffect } from 'react'; // 这行必须写，不然报错，可能是eslint搞事情。
import {
  Row,
  Input,
  Form,
  DatePicker,
  Modal,
  message,
  TreeSelect,
  Radio,
  Select,
  Tree,
  InputNumber
} from 'antd';
const { confirm } = Modal;
const { RangePicker } = DatePicker;
const { TreeNode } = TreeSelect;
const { Option } = Select;
const { TextArea } = Input;

import {listDept,deeps2,addDept } from '@/api/system/dept.js'
import {handleTree} from '@/utils/ruoyi.js'

const AddDept = ({
  visible,
  onCancel,
  oninitList,
}) => {
  const [form] = Form.useForm();
  useResetFormOnCloseModal({
    form,
    visible,
  });
  const [treeData, settreeData] = useState([])
  const [menuOptions,setMenuOptions]=useState([])     // 菜单树选项

  //点击添加用户
  const onOk = () => {
    form.submit();
  }
  useLayoutEffect(() => {
    if(visible){
      getTreeselect()
    }
  }, [visible]);




      /** 查询菜单下拉树结构 */
   const  getTreeselect=()=>{
    listDept().then(response => {
       settreeData(deeps2(handleTree(response.data, "deptId")))
     });
  }


  //表单提交方法
  const onFinish = values => {
    addDept(values).then(res => {
      if (res.code == 200) {
        message.success('添加部门');
        onCancel();
        oninitList();
      } else if (res.code == 500) {
        message.warning(res.msg);
      }
    });
  };

  const layout = {
    labelCol: { span: 6 },
    wrapperCol: { span: 16 },
  };


  return (
    <Modal
      title="添加部门"
      visible={visible}
      onOk={onOk}
      onCancel={onCancel}
      cancelText="取消"
      okText="确定"
    >
      <Form
        {...layout}
        form={form}
        labelAlign="right"
        layout="horizontal"
        onFinish={onFinish}
      >
        <Form.Item
          label="上级部门"
          rules={[{ required: true,message:'上级部门不能为空' }]}
          name="parentId"

        >
            {treeData.length && (
              <TreeSelect
                treeData={treeData}
                placeholder="选择上级部门"
                value="若依科技"
              ></TreeSelect>
            )}
        </Form.Item>

        <Form.Item label="部门名称"
          name="deptName"
          rules={[{ required: true, message: '部门名称不能为空' }]}
        >
           <Input placeholder="请输入部门名称"></Input>
        </Form.Item>

        <Form.Item label="显示排序"
          name="orderNum"
          rules={[{ required: true, message: '显示排序不能为空' }]}
        >
                  <InputNumber ></InputNumber>
        </Form.Item>
        <Form.Item label="负责人"
             name="leader"
        >
                  <Input placeholder="请输入负责人"></Input>
        </Form.Item>
        <Form.Item label="联系电话"
           name="phone"
        >
                <Input placeholder="请输入联系电话"></Input>
        </Form.Item>
        <Form.Item label="邮箱"
           name="email"
        >
              <Input placeholder="请输入邮箱"></Input>
        </Form.Item>
        <Form.Item label="部门状态"
            name="status"
        >
          <Radio.Group>
          <Radio value={'0'}>正常</Radio>
          <Radio value={'1'}>停用</Radio>
          </Radio.Group>
        </Form.Item>
      </Form>
    </Modal>
  );
};


const useResetFormOnCloseModal = ({ form, visible }) => {
  const prevVisibleRef = useRef();
  useEffect(() => {
    prevVisibleRef.current = visible;
  }, [visible]);
  const prevVisible = prevVisibleRef.current;
  useEffect(() => {
    if (!visible && prevVisible) {
      form.resetFields();
    }
  }, [visible]);
};

export default AddDept;
